require 'adauth'
require 'jwt'

class AuthDomain
  def self.auth uid, passwd
    Adauth.configure do |c|
      c.domain  = ENV["AD_DOMAIN"] || 'cloud.ccde.cnpc'
      c.server  = ENV["AD_SERVER"] || '10.132.5.6'
      c.port    = ENV["AD_PORT"] || 389
      c.base    = ENV["AD_BASE"] || 'dc=cloud,dc=ccde,dc=cnpc'
      c.query_user      = ENV["AD_USER"] || 'DellNAS'
      c.query_password  = ENV["AD_PASSWD"] || 'ZAQ!1234'
    end

    Adauth.authenticate(uid, passwd)  # [true | false]
  end

  def self.gen_token uid
    JWT.encode({uid: uid}, Secret.current.value, "HS256")
  end
end
